﻿using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using CredentialsManager.Common;
using MVVMFoundation;
using System.Windows.Input;

namespace CredentialsManager.ViewModels
{
    public class MainWindowViewModel : ViewModelBase
    {
        #region Private member variables

        private ReadOnlyObservableCollection<WorkspaceViewModelBase> m_Workspaces;
        private ManagementViewModel m_ManagementViewModel;
        private ServiceViewModel m_ServiceViewModel;

        #endregion

        #region Constructors

        public MainWindowViewModel()
            : this(CredentialsManager.Properties.Resources.MainWindowTitle)
        { }

        public MainWindowViewModel(string displayName)
            : base()
        {
            this.DisplayName = displayName;
            m_ManagementViewModel = new ManagementViewModel();
            m_ServiceViewModel = new ServiceViewModel();

            var workspaces = new ObservableCollection<WorkspaceViewModelBase>();
            workspaces.Add(m_ManagementViewModel);
            workspaces.Add(m_ServiceViewModel);
            this.Workspaces = new ReadOnlyObservableCollection<WorkspaceViewModelBase>(
                workspaces);
            this.ActiveWorkspace = m_ManagementViewModel;

            m_ServiceViewModel.Select();
        }

        #endregion

        #region Public properties

        public ReadOnlyObservableCollection<WorkspaceViewModelBase> Workspaces
        {
            get
            {
                return m_Workspaces;
            }
            private set
            {
                m_Workspaces = value;
                RaisePropertyChanged(MethodBase.GetCurrentMethod().GetPropertyName());
            }
        }

        public WorkspaceViewModelBase ActiveWorkspace
        {
            get
            {
                WorkspaceViewModelBase activeWorkspace = null;

                if (Workspaces != null)
                {
                    activeWorkspace = Workspaces.Where(w => w.IsSelected).FirstOrDefault();
                }

                return activeWorkspace;
            }
            set
            {
                if (value != null && Workspaces != null)
                {
                    foreach (var workspace in Workspaces)
                    {
                        if (workspace != value)
                        {
                            workspace.IsSelected = false;
                        }
                        else
                        {
                            workspace.IsSelected = true;
                        }
                    }
                }
                RaisePropertyChanged(MethodBase.GetCurrentMethod().GetPropertyName());
            }
        }

        public ManagementViewModel ManagementViewModel
        {
            get
            {
                return m_ManagementViewModel;
            }
        }

        public ServiceViewModel ServiceViewModel
        {
            get
            {
                return m_ServiceViewModel;
            }
        }

        #endregion

        #region Private methods

        [MediatorMessageSink(MediatorMessages.SelectServiceViewModel)]
        private void HandleSelectServiceViewModel(bool message)
        {
            if (message)
            {
                foreach (var workspace in Workspaces)
                {
                    if (workspace.GetType() == typeof(ServiceViewModel))
                    {
                        this.ActiveWorkspace = workspace;
                        break;
                    }
                }
            }
        }

        #endregion
    }
}
